<template>
  <div class="user">
    <div class="left">
      <div class="img">
        <img
          :src=" newSrc ? 'http://localhost:3000'+newSrc :oldSrc"
          alt=""
        >
      </div>
      <h1>{{info.username}}</h1>
    </div>
    <div class="right">
      <el-form
        :model="ruleForm"
        :rules="rules"
        ref="ruleForm"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="账号">
          <el-input
            :disabled="true"
            v-model="ruleForm.username"
          ></el-input>
        </el-form-item>
        <el-form-item
          label="性别"
          prop="gender"
        >
          <el-select
            v-model="ruleForm.gender"
            placeholder="请选择性别"
          >
            <el-option
              label="男"
              value="1"
            ></el-option>
            <el-option
              label="女"
              value="0"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item
          label="头像"
          prop="avatar"
        >
          <el-upload
            class="avatar-uploader"
            action="https://jsonplaceholder.typicode.com/posts/"
            :show-file-list="false"
            :auto-upload="false"
            :on-change="changeImg"
          >
            <img
              v-if="ruleForm.avatar"
              :src="ruleForm.avatar"
              class="avatar"
            >
            <i
              v-else
              class="el-icon-plus avatar-uploader-icon"
            ></i>
          </el-upload>
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            @click="updateUser('ruleForm')"
          >更新</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import axios from "axios";
import { mapState } from "vuex";
export default {
  name: "User",
  data() {
    return {
      ruleForm: {
        id: "",
        username: "",
        gender: "",
        avatar: "",
      },
      oldSrc:
        "https://cube.elemecdn.com/3/7c/3ea6beec64369c2642b92c6726f1epng.png",
      newSrc: "",
      rules: {
        gender: [{ required: true, message: "请选择性别", trigger: "change" }],
        avatar: [{ required: true, message: "请上传图片", trigger: "change" }],
      },
    };
  },
  mounted() {
    this.ruleForm.username = this.info.username;
    this.ruleForm.gender = this.info.gender;
    this.ruleForm.id = this.info.id;
    this.newSrc = this.info.avatar || "";
  },
  computed: {
    ...mapState({
      info: (state) => {
        return state.user.info;
      },
    }),
  },
  methods: {
    changeImg(e) {
      this.ruleForm.avatar = URL.createObjectURL(e.raw);
      this.ruleForm.file = e.raw;
    },
    updateUser(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          // 要对象形式所以要实例化一下
          const params = new FormData();
          for (let i in this.ruleForm) {
            params.append(i, this.ruleForm[i]);
          }
          axios({
            url: "/api/user/upload",
            method: "post",
            data: params,
            handers: { "Content-Type": "multipart/form-data" },
          }).then((result) => {
            if (result.data.code === "200") {
              this.$store.commit("ADD_INFORMATION", result.data.data);
            }
            this.$message({
              message: "更新成功",
              type: "success",
              duration: 1000
            });
            // console.log(result);
          });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
  },
  watch: {
    info: {
      handler(newval) {
        this.ruleForm.username = this.info.username;
        this.ruleForm.gender = this.info.gender;
        this.ruleForm.id = this.info.id;
        this.newSrc = this.info.avatar || "";
      },
    },
  },
};
</script>

<style lang = "less" scoped>
.user {
  margin: 0 auto;
  width: 800px;
  padding: 0 60px;
  box-sizing: border-box;
  display: flex;
  .left {
    margin-right: 20px;
    text-align: center;
    width: 300px;
    height: 300px;
    background-color: #fafafa;
    .img {
      margin-top: 30px;
      margin-bottom: 20px;
      text-align: center;
      img {
        width: 150px;
        height: 150px;
        border-radius: 50%;
        overflow: auto;
      }
    }
  }
  .right {
    padding: 30px;
    background-color: #fafafa;
    .el-form-item {

      .el-button {
        margin-top: 10px;
      }
    }
    
    ::v-deep .avatar-uploader .el-upload {
      border: 1px dashed #d9d9d9;
      border-radius: 6px;
      cursor: pointer;
      position: relative;
      overflow: hidden;
    }
    ::v-deep .avatar-uploader .el-upload:hover {
      border-color: #409eff;
    }
    ::v-deep .avatar-uploader-icon {
      font-size: 28px;
      color: #8c939d;
      width: 178px;
      height: 178px;
      line-height: 178px;
      text-align: center;
    }
    ::v-deep .avatar {
      width: 178px;
      height: 178px;
      display: block;
    }
  }
}
</style>